package com.zzh.net;

import android.content.Context;

import com.zzh.exception.InitException;
import com.zzh.net.bean.ResponseBean;
import com.zzh.net.net.NetObserver;
import com.zzh.net.net.NetRetrofitManager;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-08-29
 * @desc
 */
public class Net {

    private static class NetHelper {
        static Net net = new Net();
    }

    private Net() {

    }

    public static Net getInstance() {
        return NetHelper.net;
    }

    private Context mContext;
    private List<String> baseUrls;

    public Context getContext() {
        if (mContext == null) {
            throw new InitException("Net 没有传入context");
        }
        return mContext;
    }

    public List<String> getBaseUrls() {
        return baseUrls;
    }

    static class Buider {
        private List<String> baseUrls;

        /**
         * 必传
         *
         * @param baseUrls
         * @return
         */
        public Buider setBaseUrls(List<String> baseUrls) {
            this.baseUrls = baseUrls;
            return this;
        }

        public Net buider(Context context) {
            Net net = Net.getInstance();
            net.mContext = context;
            net.baseUrls = baseUrls;
            return net;
        }
    }

    public static NetRetrofitManager getNetRetrofitManager() {
        return NetRetrofitManager.getInstance();
    }


    public static void bind(Observable<ResponseBean> observable, NetObserver<ResponseBean> observer){
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

}
